package com.yanyeori.nacosfeign.controller;

import com.yanyeori.framework.core.context.TraceContext;
import com.yanyeori.framework.core.context.UserContext;
import com.yanyeori.framework.core.model.WebResponse;
import com.yanyeori.framework.core.util.JacksonUtil;
import com.yanyeori.nacosfeign.client.FeignAClient;
import com.yanyeori.nacosfeign.client.FeignBClient;
import com.yanyeori.nacosfeign.context.AContext;
import com.yanyeori.nacosfeign.context.AContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 测试接口
 * @date 20/8/07 10:29
 */
@RestController
public class TestController implements FeignAClient {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private FeignBClient feignBClient;

    @Override
    public WebResponse<String> rest(String param) {
        log.info("traceId: {}", TraceContext.get(false).getTraceId());
        log.info("param：{}, UserContext：{}", param, JacksonUtil.toJson(UserContext.get()));
        log.info("param：{}, AContext：{}\n", param, JacksonUtil.toJson(AContextHolder.getAContext()));
        return new WebResponse<String>().data("nacos-feign-a被调用，参数：" + param);
    }

    @Override
    public WebResponse<AContext> rest1(String param) {
        log.info("param：{}, UserContext：{}", param, JacksonUtil.toJson(UserContext.get()));
        log.info("param：{}, AContext：{}\n", param, JacksonUtil.toJson(AContextHolder.getAContext()));
        AContext aContext = new AContext();
        aContext.setName("nacos-feign-a被调用，参数：" + param);
        return WebResponse.ok(aContext).message("nacos-feign-a被调用，参数：" + param);
    }

    @PostMapping("/testController/test")
    public WebResponse test() {
        //调用远程服务nacos-feign-b-business
        WebResponse response = feignBClient.rest("feign a调用b参数");
        log.info("远程服务返回：{}", response);
        return response;
    }
}
